<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="../js/hyl/core/safe/sm2.js"></script>
		<script src="../js/hyl/core/safe/sm3.js"></script>
		<script src="../js/hyl/core/safe/sm4.js"></script>
	</head>
	<body>
	</body>
	<script>
		// 或者

		let keypair = sm2.generateKeyPairHex();
		// 公钥
		let publicKey = keypair.publicKey;
		console.log("publicKey", publicKey)
		// 私钥
		let privateKey = keypair.privateKey;
		console.log("privateKey", privateKey)

		publicKey =
			"045a77572850a59b6ce01ca368dd5b4773e881554652e81fa21e49e95414950359be062101d030f07c9ae635f2171392496f717bba247acb6faa38d28d551619ae"
		privateKey = "2b64f34cb7a7ca1b8a1a19a809ba4e12238fac722459abe883d99d7b895a8a46"
		var 密文 =
			"2d25308cf92b8dd7f90d67f6c663c72469917e86e298b69b33a1a9dd13b434e9cbcd031f2311ea5fe7fd317d02b4e4b0c86d7eb70ccfe3a8df78b544221dbc8029fd7743753b18c42efc14052857bd09977abf770ba0d76ac6c47283ec421108d5bc0a5f7efc1d88bef6d16bca53fe58fcb7726a"
		data1 = sm2.doDecrypt(密文, privateKey);
		console.log("sm2解密1", data1)

		//	公钥加密
		let data = "顶顶顶顶!@^&*()_";
		// const sm2 = require('sm-crypto').sm2;
		var data2 = sm2.doEncrypt(data, publicKey);
		console.log("sm2加密2", data2)
		//	私钥解密

		//// const sm2 = require('sm-crypto').sm2;


		// var data3 = sm2.doDecrypt(data2, privateKey);
		// console.log("sm2解密2", data3)

		// let cipherMode = 1 //1-C1C3C2,0-C1c2C3,默认为1
		// let encryptData = sm2.doEncrypt(data, publicKey, cipherMode) //加密结果
		// console.log("sm2加密3", encryptData)
		// let decryptData = sm2.doDecrypt(encryptData, privateKey, cipherMode) //解密结果
		// console.log("sm2解密3", decryptData);



		// cipherMode = 0;
		// encryptData = sm2.doEncrypt(data, publicKey, 0) //加密结果，输入数组
		// console.log("sm2加密4", encryptData)
		// decryptData = sm2.doDecrypt(encryptData, privateKey, 0, {
		// 	output: 'array'
		// }) //解密结果
		// console.log("sm2解密4", decryptData)
		// decryptData = sm2.doDecrypt(encryptData, privateKey, 0) //解密结果
		// console.log("sm2解密4", decryptData)


		let msg = "12535675如果不给他联合国v宝宝黑色v不甘落后的";
		//方法1:纯签名+生成椭圆曲线点
		// let sigvalueHex = sm2.doSignature(msg, privateKey) //签名
		// console.log("sm2签名", sigvalueHex)
		// let verifyResult = sm2.doVerifySignature(msg, sigvalueHex, publicKey) // 验签结果
		// console.log("sm2签名验证1", verifyResult)


		//方法2:纯签名
		// let sigvalueHex2 = sm2.doSignature(msg, privateKey, {
		// 	pointPool: [sm2.getPoint(), sm2.getPoint(), sm2.getPoint(), sm2.getPoint()], //传入事先已生成好的椭圆曲线点，可加快签名速度
		// }) //签名
		// console.log("sm2签名", sigvalueHex2)
		// let verifyResult2 = sm2.doVerifySignature(msg, sigvalueHex2, publicKey) // 验签结果

		// console.log("sm2签名验证2", verifyResult2)


		//方法3: 纯签名+生成椭圆曲线点+der编解码
		// let sigvalueHex3 = sm2.doSignature(msg, privateKey, {
		// 	der: true,
		// }) //签名
		// console.log("sm2签名", sigvalueHex3)
		// let verifyResult3 = sm2.doVerifySignature(msg, sigvalueHex3, publicKey, {
		// 	der: true,
		// }) //验签结果
		//  console.log("sm2签名验证3", verifyResult3)


		//方法4: 纯签名+生成椭圆曲线点+sm3杂凑
		// let sigvalueHex4 = sm2.doSignature(msg, privateKey, {
		// 	hash: true,
		// }) //签名
		// console.log("sm2签名", sigvalueHex4)
		// let verifyResult4 = sm2.doVerifySignature(msg, sigvalueHex4, publicKey, {
		// 	hash: true,
		// }) //验签结果
		// console.log("sm2签名验证4", verifyResult4)



		//方法5: 纯签名+生成椭圆曲线点+s3杂凑（不做公钥推导）
		// let sigvalueHex5 = sm2.doSignature(msg, privateKey, {
		// 	hash: true,
		// 	publicKey, //传入公钥的话，可以去掉sm3杂凑中推导公钥的过程，速度会比纯签名+生成椭圆曲线点+sm3杂凑快
		// })
		// console.log("sm2签名", sigvalueHex5)
		// let verifyResult5 = sm2.doVerifySignature(msg, sigvalueHex5, publicKey, {
		// 	hash: true,
		// 	publicKey,
		// })
		// console.log("sm2签名验证5", verifyResult5)


		//方法6:纯签名+生成椭圆曲线点+sm3杂凑+不做公钥推+添加userId(长度小于8192)
		//默认userId值为1234567812345678
		// let sigvalueHex6 = sm2.doSignature(msg, privateKey, {
		// 	hash: true,
		// 	publicKey,
		// 	userId: 'testUserId',
		// });
		// console.log("sm2签名", sigvalueHex6)
		// let verifyResult6 = sm2.doVerifySignature(msg, sigvalueHex6, publicKey, {
		// 	hash: true,
		// 	userId: 'testUserId',
		// })
		// console.log("sm2签名验证6", verifyResult6)



		//sm3示例
		let hashData = sm3('ab3421532151245 文人35c') //杂凑
		console.log('sm3', hashData)


sm4key= "D28750D59400DBBFF47DBB9840CFCFC4" ;
var mm="d4f287ff81c49e76e58388497f726330";

		dedata = sm4.decrypt(mm, sm4key);
		console.log('sm4解密1', dedata);

		//sm4示例
		msg = 'hel1o world!我是juneandgreen.' //可以为utf8串或字节数组
		const key = 'abcdefabcdef98765432109876543210' //可以为16进制串或字节数组，要求为128比特
		encryptData = sm4.encrypt(msg, key); //加密，默认输出I6进制字符串，默认使用pkcs#7填充（传pkcs#5 也会走 pkcs#7 填充）
		console.log('sm4加密', encryptData)
		dedata = sm4.decrypt(encryptData, key);
		console.log('sm4解密2', dedata);
		// encryptData=sm4.encrypt(msg,key,[padding:'none'});//加密，不使用padding
		// encryptData=sm4.encrypt(msg,key,{padding:'none',output:'array'})//加，不使用padding,输出为字节数组
		// encryptData= sm4.encrypt(msg,key,{mode:'cbc',iv:'fedcba98765432100123456789abcdef'})//加密，abc模式
	</script>
</html>